//
//  PartManager.m
//  随拍
//
//  Created by lanou3g on 16/1/20.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "PartManager.h"
#define Base_URL @"http://api.breadtrip.com/destination"
#import "PartModel.h"

@interface PartManager ()

@property(nonatomic,strong)NSString *string;




@end


@implementation PartManager

+( PartManager*)sharedHomeManager{
    static PartManager *manager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[PartManager alloc]init];
        [manager requesetNetWork];
        
    });
    
    return manager;
}




-(void)requesetNetWork{
    
    
    
    NSURL *url = [NSURL URLWithString:Base_URL];
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        for (NSDictionary *dic in array) {
            
            PartModel *partModel = [[PartModel alloc] init];
            
            [partModel setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:partModel];
            
            
        }
        
        for ( int i =0; i<array.count; i++) {
            self.array = array[i][@"images"];
          
            
            for (NSDictionary *dict in self.array) {
               
                self.partModel = [[PartModel alloc] init];
                
                [_partModel setValuesForKeysWithDictionary:dict];
                
                [self.homeArray addObject:_partModel];
                
            }
            
            
            
        }
        
        
        
        NSLog(@"%@",self.homeArray);
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.updateUIa();
            
        });
        
        
        
    }];
    
    [task resume];
    
}

-(NSMutableArray *)homeArray{
    if (!_homeArray) {
        self.homeArray = [NSMutableArray array];
    }
    return _homeArray;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        self.dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


-(PartModel *)returnModelByIndex:(NSInteger)index{
    return [self.dataArray objectAtIndex:index];
}


@end
